function [WeiDist] = Principal_A2(Par2)
n = 50;
load ParEst_eh1_time0_Mex_nokids ParEst_eh1_time0_Mex_nokids
load matrix_time1_age0_eh1_comp0.out 
%load matrix_time1_eh1_comp0.out
load stock_time2_age0_eh1_comp0_anyhs0.out
load stock_time2_age0_eh1_comp0_anyhs1.out
load stock_time2_age0_eh1_comp0.out
load stock_time2_eh1_comp0_anyhs0.out
load stock_time2_eh1_comp0_anyhs1.out
load stock_time2_eh1_comp0.out
load W_time2_age0_eh1_comp0.out
load W_time2_age0_eh1_comp01.out
load W_time2_eh1_comp0.out

Par1 = zeros(35,1);
Par1(1:29) = ParEst_eh1_time0_Mex_nokids;
Par1(21) = 10^ParEst_eh1_time0_Mex_nokids(21);
Par1(22:27)=1000*ParEst_eh1_time0_Mex_nokids(22:27);
Par1(30) = 500*Par2(1);
Par1(31) = 500*Par2(2);
Par1(32:35)=Par2(3:6);

% Health transitions and unhealthy proportion
phl=matrix_time1_age0_eh1_comp0(1);
plh=matrix_time1_age0_eh1_comp0(2);
prob_l=matrix_time1_age0_eh1_comp0(3);
 
w_f_g=W_time2_age0_eh1_comp01; % Read wages, g and f distributions
wf_1=w_f_g(:,1);        % Wage in formal sector
wi_1=w_f_g(:,2);        % Wage in informal sector
wf_2=w_f_g(:,7);      % Wage in formal sector
wi_2=w_f_g(:,8);      % Wage in informal sector
 
w_mom=W_time2_age0_eh1_comp0; % Read wage moments
meanwi_1=w_mom(1); meanwf_1=w_mom(2);   meanwi_2=w_mom(3);  meanwf_2=w_mom(4);  sdwi_1=w_mom(5); sdwf_1=w_mom(6);  sdwi_2=w_mom(7);   sdwf_2=w_mom(8);
p10wi_1=w_mom(9);  p10wf_1=w_mom(10); p10wi_2=w_mom(11); p10wf_2=w_mom(12); p25wi_1=w_mom(13); p25wf_1=w_mom(14); p25wi_2=w_mom(15); p25wf_2=w_mom(16); 
p50wi_1=w_mom(17); p50wf_1=w_mom(18); p50wi_2=w_mom(19); p50wf_2=w_mom(20); p75wi_1=w_mom(21); p75wf_1=w_mom(22); p75wi_2=w_mom(23); p75wf_2=w_mom(24); 
p90wi_1=w_mom(25); p90wf_1=w_mom(26); p90wi_2=w_mom(27); p90wf_2=w_mom(28); minwi_1=w_mom(29); minwf_1=w_mom(30); minwi_2=w_mom(31); minwf_2=w_mom(32); 
maxwi_1=w_mom(33); maxwf_1=w_mom(34); maxwi_2=w_mom(35); maxwf_2=w_mom(36);
 
wage_moments_data=[meanwi_1; meanwf_1; meanwi_2; meanwf_2; sdwi_1; sdwf_1; sdwi_2; sdwf_2]; % only using mean and stdev

stock_mom_h=stock_time2_age0_eh1_comp0_anyhs0(1:9); % Read stocks [FF_h;FI_h;FN_h;IF_h;NF_h;II_h;IN_h;NI_h;NN_h]
stock_h=stock_mom_h';
 
stock_mom_l=[stock_time2_age0_eh1_comp0_anyhs1(10),stock_time2_age0_eh1_comp0_anyhs1(11),...
    stock_time2_age0_eh1_comp0_anyhs1(5),stock_time2_age0_eh1_comp0_anyhs1(8),...
    stock_time2_age0_eh1_comp0_anyhs1(9)]; % Read stocks [FX_l;IX_l;NF_l;NI_l;NN_l]

stock_mom_agg=stock_time2_age0_eh1_comp0_anyhs0(1:9).*(1-prob_l)+stock_time2_age0_eh1_comp0_anyhs1(1:9).*prob_l;
stock_agg=stock_mom_agg';
 
% Bootstrap variances
stock_mom_boot_h=stock_time2_eh1_comp0_anyhs0(:,1:9);
HHstates_data_boot_h=var(stock_mom_boot_h)';
stock_mom_boot_l = zeros(100,5);
stock_mom_boot_l = [stock_time2_eh1_comp0_anyhs1(:,10),stock_time2_eh1_comp0_anyhs1(:,11),...
    stock_time2_eh1_comp0_anyhs1(:,5),stock_time2_eh1_comp0_anyhs1(:,8),stock_time2_eh1_comp0_anyhs1(:,9)];

stock_mom_boot_agg=stock_time2_eh1_comp0_anyhs0(:,1:9).*(1-prob_l)+stock_time2_eh1_comp0_anyhs1(:,1:9).*prob_l;
HHstates_data_boot_agg=var(stock_mom_boot_agg)';
HHstates_data_boot=[HHstates_data_boot_h;HHstates_data_boot_agg]; % stock variances

w_mom_var=var(W_time2_eh1_comp0)';
meanwi_1v=w_mom_var(1); meanwf_1v=w_mom_var(2);   meanwi_2v=w_mom_var(3);  meanwf_2v=w_mom_var(4);  sdwi_1v=w_mom_var(5); sdwf_1v=w_mom_var(6);  sdwi_2v=w_mom_var(7);   sdwf_2v=w_mom_var(8);
p10wi_1v=w_mom_var(9);  p10wf_1v=w_mom_var(10); p10wi_2v=w_mom_var(11); p10wf_2v=w_mom_var(12); p25wi_1v=w_mom_var(13); p25wf_1v=w_mom_var(14); p25wi_2v=w_mom_var(15); p25wf_2v=w_mom_var(16); 
p50wi_1v=w_mom_var(17); p50wf_1v=w_mom_var(18); p50wi_2v=w_mom_var(19); p50wf_2v=w_mom_var(20); p75wi_1v=w_mom_var(21); p75wf_1v=w_mom_var(22); p75wi_2v=w_mom_var(23); p75wf_2v=w_mom_var(24); 
p90wi_1v=w_mom_var(25); p90wf_1v=w_mom_var(26); p90wi_2v=w_mom_var(27); p90wf_2v=w_mom_var(28); minwi_1v=w_mom_var(29); minwf_1v=w_mom_var(30); minwi_2v=w_mom_var(31); minwf_2v=w_mom_var(32); 
maxwi_1v=w_mom_var(33); maxwf_1v=w_mom_var(34); maxwi_2v=w_mom_var(35); maxwf_2v=w_mom_var(36);
wage_data_boot = [meanwi_1v; meanwf_1v; meanwi_2v; meanwf_2v; sdwi_1v; sdwf_1v; sdwi_2v; sdwf_2v];
 
boot_vector = [wage_data_boot; HHstates_data_boot];

%HHstates_data_reduced=[stock_h;stock_l]; % [FF_h;FI_h;FN_h;IF_h;NF_h;II_h;IN_h;NI_h;NN_h;FX_l;IX_l;NF_l;NI_l;NN_l]  joint-job state
HHstates_data=[stock_h;stock_agg]; % [FF_h;FI_h;FN_h;IF_h;NF_h;II_h;IN_h;NI_h;NN_h;FF_agg;FI_agg;FN_agg;IF_agg;NF_agg;II_agg;IN_agg;NI_agg;NN_agg]  joint-job state

 % solve dist of wage offers, value functions and simulate trajectory (MSM)
[Ff_1,ff_1,Fi_1,fi_1,Ff_2,ff_2,Fi_2,fi_2]=solve_wage_offer_dist_2(Par1,wf_1,wi_1,wf_2,wi_2,n);
[Wff_h,Wfi_h,Wfn_h,Wif_h,Wnf_h,Wii_h,Win_h,Wni_h,Wnn_h,Wff_l,Wfi_l,Wfn_l,Wif_l,Wnf_l,Wii_l,Win_l,Wni_l,Wnn_l,iterVF]=solve_value_function_A1_1(Par1,...
    Ff_1,wf_1,Fi_1,wi_1,Ff_2,wf_2,Fi_2,wi_2,n);
if iterVF>=401
WeiDist = 1000^10;
else
[HHstates,wage_moments,transitions,phl,plh]=data_simulation_A12_end(Par1,ff_1,wf_1,fi_1,...
    wi_1,ff_2,wf_2,fi_2,wi_2,Wff_h,Wfi_h,Wfn_h,Wif_h,Wnf_h,Wii_h,Win_h,Wni_h,Wnn_h,Wff_l,Wfi_l,Wfn_l,Wif_l,Wnf_l,Wii_l,Win_l,Wni_l,Wnn_l);
MatCov = zeros(26);
for i=1:26
    MatCov(i,i) = 1/(boot_vector(i));
end
MomSim = zeros(26,1);
MomData = zeros(26,1);
MomData = [wage_moments_data; HHstates_data];
MomSim =  [wage_moments;HHstates];
WeiDist = 15*(transpose(MomData - MomSim))*MatCov*(MomData-MomSim)
end
end
